package main

import (
	"fmt"
	"git.supremind.info/product/box/sdk/core"
	"git.supremind.info/product/box/vehicle/common"
	"git.supremind.info/product/box/vehicle/config"
	"git.supremind.info/product/box/vehicle/global"
	"git.supremind.info/product/box/vehicle/internal"
	"git.supremind.info/product/box/vehicle/internal/dao"
	"git.supremind.info/product/box/vehicle/internal/job"
	"git.supremind.info/product/box/vehicle/internal/kafka"
	"git.supremind.info/product/box/vehicle/internal/s3"
	"git.supremind.info/product/box/vehicle/internal/util"
	"git.supremind.info/product/box/vehicle/internal/vmr"
	"git.supremind.info/product/box/vehicle/internal/wsserver"
	"github.com/joho/godotenv"
	"github.com/sirupsen/logrus"
	"os"
	"time"
)

func init() {
	core.PrintSdkBanner()

	fmt.Println("------------------ main init ------------------")

	global.Env = initEnv()
	fmt.Println("env: ", os.Getenv("ENV"))

	// 加载config.yaml
	err := loadConfigYaml()
	if err != nil {
		logrus.Errorf("init setupSetting Error : %v", err)
	}

	global.InitConfig()

	//初始化日志
	common.InitLogger(global.AppSetting)

	dao.InitSqlite()
}

func main() {
	fmt.Println("------------------ main ------------------")

	//启动清理socket集合
	go wsserver.RemoveModelSocketMapLoop()
	go wsserver.RemoveModelStatusSocketMapLoop()

	InitKafkaAndS3()

	job.GetVmrModelStatus()
	job.GetVmrModelData()

	//订阅纳管和取消纳管通知
	go job.SubscribeVmrPlatformCommandTopic()
	//推送数据到平台端
	go job.PushModelDataToPlatform()
	//清除数据
	go job.ClearVehicleData()

	// 初始化路由
	r := internal.NewRouter()
	err := r.Run(":" + global.ServerSetting.HttpPort)
	if err != nil {
		fmt.Println("服务器启动失败！")
	}
}

func InitKafkaAndS3() {
	// 调vmr获取盒子信息
	var boxInfo = vmr.BoxInfo{}
	err := util.Get(fmt.Sprintf("%s/v1/box", global.VmrSetting.Host), &boxInfo)
	if err != nil {
		common.Logger.Error(err)
		return
	} else if boxInfo.Code != 0 {
		return
	}

	// 调vmr获取纳管信息  {"code":0,"data":{"box_id":"","kafka_brokers":null,"s3":null}}
	var platformInfo = vmr.PlatformInfo{}
	err = util.Get(fmt.Sprintf("%s/v1/box/platform_info", global.VmrSetting.Host), &platformInfo)
	fmt.Println(platformInfo)
	if err != nil {
		common.Logger.Error(err)
		return
	} else if platformInfo.Code != 0 {
		return
	}

	if boxInfo.Data.BoxId == platformInfo.Data.BoxId {
		kafkaBrokers := platformInfo.Data.KafkaBrokers
		var platformS3 = platformInfo.Data.S3
		if kafkaBrokers != nil && len(kafkaBrokers) > 0 {
			kafka.GetInstance().ConnectKafka(kafkaBrokers)
			s3.PlatformInstance().InitMinioClient(platformS3)
		}
	}
}

func initEnv() string {
	// 加载.env文件配置
	err := godotenv.Load(".env")
	// 获取运行环境
	env := os.Getenv("ENV")
	if err != nil {
		fmt.Println("配置文件加载失败")
		env = "pro"
	}
	return env
}

func loadConfigYaml() error {
	mySetting, err := config.NewSetting(global.Env)
	if err != nil {
		return err
	}
	err = mySetting.ReadSection("Vmr", &global.VmrSetting)
	if err != nil {
		return err
	}
	err = mySetting.ReadSection("Server", &global.ServerSetting)
	if err != nil {
		return err
	}
	err = mySetting.ReadSection("App", &global.AppSetting)
	if err != nil {
		return err
	}
	err = mySetting.ReadSection("Database", &global.DatabaseSetting)
	if err != nil {
		return err
	}
	//将数据设置为秒单位
	global.ServerSetting.ReadTimeout *= time.Second
	global.ServerSetting.WriteTimeout *= time.Second
	return nil
}
